package com.guoyu.project.web.api;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.utils.UploadUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Article;
import com.guoyu.project.model.Attachment;
import com.guoyu.project.model.Banner;
import com.guoyu.project.model.Project_Manage;
import com.guoyu.project.model.UserInfo;
import com.guoyu.project.model.enums.ArticleMinorType;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.service.FileUploadService;
import com.guoyu.project.service.Project_ManageService;

@Controller
@RequestMapping("/api/PManage")
public class ProjectManageApiController extends BaseApiController{
	
	    //日志
	    private final static Logger logger = LoggerFactory.getLogger(ProjectManageApiController.class);
        
	    @Resource
	    private Project_ManageService project_ManageService;
	    
	    @Resource
	    private FileUploadService fileUploadService;
	    /**
	     * pm信息列表页
	     *
	     * @return
	     */

	    @RequestMapping("pmlist")
	    @ResponseBody
	    public APIResponse list(@RequestParam(defaultValue = "0") int offset,
	            @RequestParam(defaultValue = "10") int limit) {	    
	    	Map<String, Object> params = Maps.newHashMap();
	    	 Pagination<Project_Manage> pagination = project_ManageService.queryAllProject_Manage(offset, limit, params);
	         return APIResponse.returnSuccess(pagination);
	        
	    }

	    /**
	     * 保存今日资讯信息
	     *
	     * @return
	     * @throws IOException 
	     */
	    @RequestMapping("save")
	    @ResponseBody
	    public APIResponse save(@RequestParam(value = "pic", required = false) MultipartFile picture,
	    		@RequestParam(value = "saveFil1", required = false)MultipartFile fiFile, 
	    		@RequestParam(value = "saveFil2", required = false)MultipartFile seFile, 
	    		@RequestParam(value = "saveFil3", required = false)MultipartFile thFile, 
	    		Project_Manage project_manage,HttpServletRequest request) {
	        logger.info("提交添加信息：{}", JsonUtil.encode(project_manage));
	        UserInfo sessionUserInfo = getCurrentUser(request);
	        int userId = sessionUserInfo.getId();
	        project_manage.setUserId(userId);    
	        //图片上传
	        String picPath = fileUploadService.upload(picture);
	        project_manage.setPicture(picPath);
	        //文件上传
	        String fifilePath=fileUploadService.upload(fiFile);
	        project_manage.setFiFile(fifilePath);
	        
	        String sefilePath=fileUploadService.upload(seFile);
	        project_manage.setSeFile(sefilePath);
	        
	        String thfilePath=fileUploadService.upload(thFile);
	        project_manage.setThFile(thfilePath);
	        
	        project_manage.setStatus(0);
	        project_ManageService.saveProject_Manage(project_manage);
	        return APIResponse.returnSuccess();
	    }
	    /**
	     * 跳到修改信息页面
	     *
	     * @return
	     */
	    @RequestMapping("query")
	    @ResponseBody
	    public APIResponse edit(Integer pId) {
	        Project_Manage project_manage = project_ManageService.queryById(pId);
	        return APIResponse.returnSuccess(project_manage);
	    }

	    /**
	     * 更新
	     *
	     * @return
	     */
	    @RequestMapping("update")
	    @ResponseBody
	    public APIResponse update(@RequestParam(value = "pic", required = false) MultipartFile picture,
	    		@RequestParam(value = "UpdateFil1", required = false)MultipartFile fiFile, 
	    		@RequestParam(value = "UpdateFil2", required = false)MultipartFile seFile, 
	    		@RequestParam(value = "UpdateFil3", required = false)MultipartFile thFile, 
	    		Project_Manage project_manage) {
	        logger.info("提交修改项目管理信息：{}", JsonUtil.encode(project_manage));
	        //图片
	        String picPath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), project_manage.getPicture());
	        project_manage.setPicture(picPath);
	        
	        //文件上传
	        String fifilePath=fileUploadService.upload(fiFile);
	        project_manage.setFiFile(fifilePath);
	        
	        String sefilePath=fileUploadService.upload(seFile);
	        project_manage.setSeFile(sefilePath);
	        
	        String thfilePath=fileUploadService.upload(thFile);
	        project_manage.setThFile(thfilePath);
	        
	        project_ManageService.updateProject_Manage(project_manage);
	        return APIResponse.returnSuccess();
	    }

	    /**
	     * 删除
	     * @return
	     */
	    @RequestMapping("delete")
	    @ResponseBody
	    public APIResponse delete(Integer pId) {
	    	project_ManageService.deleteById(pId);
	        return APIResponse.returnSuccess();
	    }
	    @RequestMapping("queryByUserId")
		@ResponseBody
		public Pagination<Project_Manage> queryByUserId(@RequestParam(defaultValue = "0") int offset,
				@RequestParam(defaultValue = "10") int limit,int userId){
			Map<String,Object> params = Maps.newHashMap();
			params.put("userId",userId);
			return project_ManageService.queryAllProject_Manage(offset, limit, params);
		}

	}